"""
MCP增强反馈处理演示脚本
展示改进后的反馈处理功能，包括优先级、安全问题处理等
"""

import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.services.mcp_service import mcp_service_manager
from src.services.mcp_feedback_enhanced_service import mcp_feedback_enhanced_service

def demo_enhanced_feedback_processing():
    """演示增强的反馈处理功能"""
    print("=== MCP增强反馈处理演示 ===\n")
    
    # 模拟服务器名称
    server_name = "test_mcp_server"
    
    # 1. 添加不同类型和优先级的反馈
    print("1. 添加不同类型和优先级的反馈")
    
    # 添加一般反馈
    result = mcp_service_manager.add_feedback(
        server_name=server_name,
        user_id="user001",
        feedback="界面响应速度较慢",
        feedback_type="performance",
        priority="high"
    )
    print(f"添加性能反馈: {result}")
    
    # 添加错误反馈
    result = mcp_service_manager.add_feedback(
        server_name=server_name,
        user_id="user002",
        feedback="查询功能偶尔返回空结果",
        feedback_type="error",
        priority="high"
    )
    print(f"添加错误反馈: {result}")
    
    # 添加安全反馈
    result = mcp_service_manager.add_feedback(
        server_name=server_name,
        user_id="user003",
        feedback="发现可能的XSS攻击向量",
        feedback_type="security",
        priority="critical"
    )
    print(f"添加安全反馈: {result}")
    
    # 添加建议反馈
    result = mcp_service_manager.add_feedback(
        server_name=server_name,
        user_id="user004",
        feedback="希望增加导出为Excel功能",
        feedback_type="suggestion",
        priority="medium"
    )
    print(f"添加建议反馈: {result}")
    
    print("\n" + "="*50 + "\n")
    
    # 2. 查看反馈历史
    print("2. 查看反馈历史")
    feedback_history = mcp_service_manager.get_feedback_history(server_name)
    for i, feedback in enumerate(feedback_history):
        print(f"反馈 {i}: 类型={feedback['feedback_type']}, 优先级={feedback['priority']}, 内容={feedback['feedback']}")
    
    print("\n" + "="*50 + "\n")
    
    # 3. 处理反馈（自动策略）
    print("3. 处理反馈（自动策略）")
    process_result = mcp_service_manager.process_feedback(server_name, strategy="auto")
    print(f"处理结果: {process_result}")
    
    print("\n" + "="*50 + "\n")
    
    # 4. 使用增强服务进行反馈分析
    print("4. 使用增强服务进行反馈分析")
    
    # 添加更多反馈用于分析
    for i in range(3):
        mcp_service_manager.add_feedback(
            server_name=server_name,
            user_id=f"user00{i+5}",
            feedback="界面响应速度较慢",
            feedback_type="performance",
            priority="medium"
        )
    
    # 分析反馈模式
    pattern_analysis = mcp_feedback_enhanced_service.analyze_feedback_patterns(server_name, days=7)
    print(f"模式分析: {pattern_analysis}")
    
    print("\n" + "="*50 + "\n")
    
    # 5. 生成行动方案
    print("5. 生成行动方案")
    action_plan = mcp_feedback_enhanced_service.generate_action_plan(server_name)
    print(f"行动方案: {action_plan}")
    
    print("\n" + "="*50 + "\n")
    
    # 6. 导出反馈洞察报告
    print("6. 导出反馈洞察报告")
    insights = mcp_feedback_enhanced_service.export_feedback_insights(server_name, format="text")
    print("反馈洞察报告:")
    print(insights["data"])
    
    print("\n=== 演示完成 ===")

if __name__ == "__main__":
    demo_enhanced_feedback_processing()